package malus.shaofeng.com.beiwomusic.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.List;

import malus.shaofeng.com.beiwomusic.R;
import malus.shaofeng.com.beiwomusic.model.BbsReply;
import malus.shaofeng.com.beiwomusic.model.HotPost;
import malus.shaofeng.com.beiwomusic.utils.BitmapHelper;
import malus.shaofeng.com.beiwomusic.utils.SPHelper;

/**
 *  话题评论列表的适配器
 */
public class TopicCommendAdapter extends BaseAdapter {
    Context context;
    List<BbsReply> data;
    HotPost hotPost;
    //回复的布局界面
//    LinearLayout llReply;
    //动画选择按钮
//    FrameLayout flButton;
    OnReplyEditListener listener;
    public TopicCommendAdapter(
            Context context, List<BbsReply> data,
            HotPost hotPost,
            LinearLayout llReply,FrameLayout flButton){
        if(context!=null){
            this.context=context;
            this.data=data;
            this.hotPost=hotPost;
//            this.llReply=llReply;
//            this.flButton=flButton;
            llReply.setVisibility(View.GONE);
            if(context instanceof OnReplyEditListener){
                this.listener=(OnReplyEditListener)context;
            }else{
                throw new IllegalArgumentException("context must implements OnReplyEditListener");
            }
        }else{
            throw new IllegalArgumentException("context must not null");
        }
    }

    @Override
    public int getCount() {
        if(data!=null){
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(data!=null){
            return data.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view=null;
        if(convertView!=null){
             view=convertView;
        }else{
            view= LayoutInflater.from(context).inflate(R.layout.item_bbs_topic_recoment_list,parent,false);
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        if(holder==null){
            holder=new ViewHolder();
            holder.nickname=(TextView)view.findViewById(R.id.bbs_reply_nickname);
            holder.refReply=(TextView)view.findViewById(R.id.bbs_reply_replyer);
            holder.content=(TextView)view.findViewById(R.id.bbs_reply_content);
            holder.ups=(TextView)view.findViewById(R.id.bbs_reply_ups);
            holder.icon=(ImageView)view.findViewById(R.id.bbs_reply_icon);
            holder.ups.setTag(position);
            holder.reply=(TextView)view.findViewById(R.id.bbs_reply_reply);
            holder.reply.setTag(position);
            view.setTag(holder);
        }
        final BbsReply reply = data.get(position);
        holder.nickname.setText(reply.getUser().getNickname());
        //当有refReply时获取对评论的回复
        if(reply.getRef_reply()!=null){
            if(reply.getRef_reply().getUser()!=null){
                holder.refReply.setVisibility(View.VISIBLE);
                String refReply=reply.getRef_reply().getUser().getNickname()+"："+reply.getRef_reply().getContent();
                holder.refReply.setText(refReply);
            }else{
                holder.refReply.setVisibility(View.GONE);
            }
        }else{
            holder.refReply.setVisibility(View.GONE);
        }
        holder.ups.setText("同感"+reply.getUps()+"");
        if(reply.isUps()){
            holder.ups.setBackgroundColor(context.getResources().getColor(R.color.umeng_activity_feedback_header_background));
            holder.ups.setClickable(false);
        }else{
            holder.ups.setBackgroundColor(context.getResources().getColor(R.color.bbs_gry));
            holder.ups.setClickable(true);
        }
        holder.content.setText(reply.getContent());
        if(reply.getUser().getFace_uri()!=null){
            //获取回复人的头像
            BitmapUtils bitmapUtils = BitmapHelper.getBitmapUtils();
            String imageUrl = BitmapHelper.getImageUrl(reply.getUser().getFace_uri());
            bitmapUtils.display(holder.icon, imageUrl);
        }

        //点击同感
        holder.ups.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //检查是否登录，如果没有登录则弹出登录框
                if(!hasLogin()){
                    return;
                }
                if(!(v.getTag() instanceof Integer)){
                    return;
                }
                final int pos=(int)v.getTag();
                final View view=v;
                if(data.get(pos).isUps()){
                    return;
                }
                HttpUtils httpUtils=new HttpUtils();
                String url = "http://kxai.cn/qms/ws/bbs/upPostReply.json";
                RequestParams params = new RequestParams();
                params.addBodyParameter("replyId",data.get(pos).getReply_id()+"");
                params.addBodyParameter("token",SPHelper.getInstance().getToken());
                httpUtils.send(HttpRequest.HttpMethod.POST, url, params,new RequestCallBack<Object>() {
                    @Override
                    public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                        data.get(pos).setUps(data.get(pos).getUps()+1);
                        data.get(pos).setUps(true);
                        TopicCommendAdapter.this.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        //点赞失败
                        Toast.makeText(context,"点赞失败>.<",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });


        //点击回复
        holder.reply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //检查是否登录，如果没有登录则弹出登录框
                if(!hasLogin()){
                    //TODO 没有登录
                    return;
                }
                if(!(v.getTag() instanceof Integer)){
                    return;
                }
                int pos=(int)v.getTag();
                listener.onOpenReply(pos);

            }
        });
        return view;
    }


    private boolean hasLogin() {
        SPHelper instance = SPHelper.getInstance();
        if(!TextUtils.isEmpty(instance.getToken())){
            return true;
        }
        return false;
    }

    private class ViewHolder{
        private TextView nickname;
        private TextView refReply;
        private TextView content;
        private TextView ups;
        private TextView reply;
        private ImageView icon;
    }
    public interface OnReplyEditListener {
        void onCloseReply();
        void onOpenReply(int i);
    }
}
